package com.happy_hao.topbiz.controller;

import com.happy_hao.topbiz.api.UserApi;
import com.happy_hao.topbiz.common.Result;
import com.happy_hao.topbiz.dto.userDto.UpdateUserRequest;
import com.happy_hao.topbiz.po.User;
import jakarta.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/topbiz/user")
public class UserController {

    @Resource
    private UserApi userApi;

    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        return Result.success().data("user", user);
    }

    @PostMapping("/update")
    public Result updateUser(@RequestBody UpdateUserRequest updateUserRequest) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        updateUserRequest.setId(user.getUserId());
        return userApi.updateUser(updateUserRequest);
    }

    @DeleteMapping("/delete")
    public Result deleteUser() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        subject.logout();
        return userApi.deleteUser(user.getUserId());
    }

}
